<?php

namespace App\Admin\Controllers;

use App\Models\Menu;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Tree;
use Illuminate\Support\MessageBag;

class MenuController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header("菜单管理");
            $content->description('&nbsp;');

            $content->body(Menu::tree());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改菜单');
            $content->description('&nbsp;');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加菜单');
            $content->description('&nbsp;');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Menu::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Menu::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->select('parent_id', "父菜单")->options(Menu::selectOptions());
            $form->select('type', '菜单类型')->options(['click' => 'click', 'view' => 'view']);
            $form->text('name', '菜单名称');
            $form->text('key', '菜单时间');
            $form->text('url', '菜单地址');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');

            $form->saving(function ($form) {

//                $error = new MessageBag([
//                    'title'   => '系统提示',
//                    'message' => '公众号一级菜单不能超过三个',
//                ]);
//
//                return back()->with(compact('error'));
            });
        });
    }

    protected function setMenuToMp()
    {
    }
}
